package com.hefeng.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.hefeng.example.domain.AppUser;
import com.hefeng.example.domain.User;
import com.hefeng.example.service.AppUserService;
import com.hefeng.common.core.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AppUserService userService;

    @PostMapping(value = "/login", produces = "application/json;charset=UTF-8")
    public AjaxResult login(@RequestBody User form) {
        String userName = form.getUsername();
        String password = form.getPassword();
        //加密
        String baseStr = userName + "%" + "gas_auth_orization" + "%" + password;
        //接口地址
        String url = "http://47.109.55.87:18081/gasApi/gas-auth/applogin";
        //base64转义
        String base64 = Base64Utils.encodeToString(baseStr.getBytes());
        System.out.println(base64);
        //构建参数map
        Map<String, Object> map = new HashMap<>();
        map.put("username", userName);
        map.put("password", password);
        map.put("autoLogin", true);
        map.put("encrypt", base64);
        //HttpEntity
        HttpEntity<?> request = new HttpEntity<>(map);
        //发送请求
        ResponseEntity<JSONObject> obj = restTemplate.postForEntity(url, request, JSONObject.class);
        System.out.println(obj.getBody());
        return AjaxResult.success(obj);
    }


    @PostMapping("/userInfo")
    public AppUser userInfo(String userid) {

        return userService.selectById(userid);
    }

    @PostMapping(value = "/userInfoInsert", produces = "application/json;charset=UTF-8")
    public AjaxResult userInfoInsert(AppUser appUser) {
        return AjaxResult.success(userService.addUser(appUser));
    }

}
